import json
from pyecharts.charts import Map
from pyecharts.options import TitleOpts, VisualMapOpts
# 读取数据
map_file = None
try:
    file_name = 'D:\\其他学习\\python学习\\python黑马课堂资料\\资料\\第1-12章资料\\资料\\可视化案例数据\\地图数据\\疫情.txt';
    map_file = open(file_name, 'r', encoding='UTF-8')
    map_file_data = map_file.read()

    # 将文件数据转为python数据
    file_data = json.loads(map_file_data)
    data_children = file_data['areaTree'][0]['children']
    provice_data_list = []
    for province_data in data_children:
        provice_name = province_data['name']
        provice_confirm = province_data['total']['confirm']
        provice_data_list.append((provice_name, provice_confirm))
    print(provice_data_list)

    map = Map()
    map.add("各省份疫情确诊数统计", provice_data_list, 'china')

    map.set_global_opts(
        title_opts=TitleOpts(title="全国疫情地图"),
        visualmap_opts=VisualMapOpts(
            is_show=True,
            is_piecewise=True,
            pieces=[
                {'min':1, 'max':9, "label":'1-9', "color":'#99FFFF'},
                {'min':10, 'max':99, "label":'10-99', "color":'#FFFFCC'},
                {'min':100, 'max':999, "label":'100-999', "color":'#FFCC66'},
                {'min':1000, 'max':9999, "label":'1000-9999', "color":'#FF9966'},
                {'min':10000, 'max':99999, "label":'1000-9999', "color":'#CC0033'},
                {'min':100000, "label":'100000+', "color":'#990033'}
            ]
        )
    )

    map.render("全国疫情确诊数统计.html")
except Exception as e:
    print(e)
    print(f'文件读取异常,读取的文件为：{file_name}')
finally:
    if map_file:
        map_file.close()